import router from './router/index'
import store from './store/index'
import { getToken } from './util/auth';
import { status } from './util/status'

router.beforeEach(async (to, from, next) => {
    if (to.name === 'login') {
        next();
        return;
    }

    if (!store.state.token) {
        const token = getToken();
        if (token) {
            store.commit('SET_TOKEN', token);
            const resContent = await store.dispatch('info');
            if (resContent.status === status.SUCCESS) {
                next()
            } else {
                next({
                    name: 'login',
                })
            }
        } else {
            next({
                name: 'login',
            })
        }

    } else {
        next()
    }
})